package cn.csust.net2.manager.client.panel;

import cn.csust.net2.manager.client.app.App;
import cn.csust.net2.manager.client.ux.BeanGridPanel;
import cn.csust.net2.manager.shared.po.Notice;
import cn.csust.net2.manager.shared.po.Student;
import cn.csust.net2.manager.shared.service.NoticeService;
import cn.csust.net2.manager.shared.service.NoticeServiceAsync;
import cn.csust.net2.manager.shared.vo.Config;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HtmlEditor;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.google.gwt.core.client.GWT;
/**
 * 
 * 公告查看panel
 * @author Administrator
 *
 */
public class NoticePanel  extends ContentPanel {
	
	private NoticeServiceAsync noticeService;
	
	private BeanGridPanel<Notice> noticeGridPanel;

	private FormPanel formPanel;
	private FormBinding formBindings;
	
	private TextField<String> titleFiled;
	private TextField<String> senderFiled;
	private TextField<String> startDateFiled ;
	private TextField<String> endDateFiled;
	private HtmlEditor content ;
	
	public NoticePanel() {
		setHeaderVisible(false);
		this.setLayout(new BorderLayout());
		init();
	}

		
		
	
	protected void init(){
		
		noticeService = GWT.create(NoticeService.class);
		createNoticeGridPanel();
		createFormPanel();

	}

	private void createNoticeGridPanel(){
		FieldSet noticeGrid = new FieldSet();   
		noticeGrid.setHeading("公告");
		noticeGrid.setLayout(new FitLayout());
		
		noticeGridPanel = new BeanGridPanel<Notice>(new Notice(),noticeService);
		noticeGridPanel.createGrid();
		noticeGridPanel.createPagingToolBar();
		
		
		
		//条件加载
		Config loadConfig = new Config();
		if(App.getUser() instanceof Student){
			Student student =  (Student)App.getUser();
			loadConfig.put("banji", student.getBanji());
		}
		noticeGridPanel.load(loadConfig);
		
		noticeGridPanel.getGrid().getSelectionModel().addListener(Events.SelectionChange,
				new Listener<SelectionChangedEvent<BeanModel>>() {
			public void handleEvent(
					SelectionChangedEvent<BeanModel> be) {
				if (be.getSelection().size() > 0) {
					formBindings.bind((ModelData) be
							.getSelection().get(0));
				} else {
					formBindings.unbind();
				}
			}
		});
		
		noticeGrid.add(noticeGridPanel);
		
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setMargins(new Margins(10,10,10,10));
		this.add(noticeGrid, centerData);  
	}
	
	private void createFormPanel(){
		
		FieldSet noticeForm = new FieldSet();   
		noticeForm.setHeading("详情");
		noticeForm.setLayout(new FitLayout());
		formPanel = new FormPanel();   
		formPanel.setBorders(false);   
		formPanel.setBodyBorder(false);   
		formPanel.setLabelWidth(55);   
		formPanel.setPadding(5);   
		formPanel.setHeaderVisible(false);   
	  
	    titleFiled = new TextField<String>();  
	    titleFiled.setName("noticeTitle");
	    titleFiled.setReadOnly(true);
	    titleFiled.setFieldLabel("主题");   
	    formPanel.add(titleFiled); 
	    
	    senderFiled = new TextField<String>(); 
	    senderFiled.setName("sender");
	    senderFiled.setReadOnly(true);
	    senderFiled.setFieldLabel("发件人");   
	    formPanel.add(senderFiled);  
	  
	    startDateFiled = new TextField<String>();
	    startDateFiled.setName("startDate");
	    startDateFiled.setReadOnly(true);
	    startDateFiled.setFieldLabel("发布日期");   
	    formPanel.add(startDateFiled);  
	    
	    endDateFiled = new TextField<String>();
	    endDateFiled.setName("endDate");
	    endDateFiled.setReadOnly(true);
	    endDateFiled.setFieldLabel("结束日期");   
	    formPanel.add(endDateFiled);   
	  
	    content = new HtmlEditor();   
	    content.setName("noticeContent");
	    content.setShowToolbar(false);
	    content.setHideLabel(true);
	    content.setEnabled(false);   
	    formPanel.add(content, new FormData("100% -53"));   
	    
	    formBindings = new FormBinding(formPanel, true);  
	    
	    noticeForm.add(formPanel);
	    
	    BorderLayoutData eastData = new BorderLayoutData(LayoutRegion.EAST,400);
	    eastData.setMargins(new Margins(10,10,10,10));
	    this.add(noticeForm, eastData);   

	}



	public BeanGridPanel<Notice> getNoticeGridPanel() {
		return noticeGridPanel;
	}




	public void setNoticeGridPanel(BeanGridPanel<Notice> noticeGridPanel) {
		this.noticeGridPanel = noticeGridPanel;
	}




	



	public TextField<String> getTitleFiled() {
		return titleFiled;
	}




	public void setTitleFiled(TextField<String> titleFiled) {
		this.titleFiled = titleFiled;
	}




	public TextField<String> getSenderFiled() {
		return senderFiled;
	}




	public void setSenderFiled(TextField<String> senderFiled) {
		this.senderFiled = senderFiled;
	}




	public TextField<String> getStartDateFiled() {
		return startDateFiled;
	}




	public void setStartDateFiled(TextField<String> startDateFiled) {
		this.startDateFiled = startDateFiled;
	}




	public TextField<String> getEndDateFiled() {
		return endDateFiled;
	}




	public void setEndDateFiled(TextField<String> endDateFiled) {
		this.endDateFiled = endDateFiled;
	}




	public FormPanel getFormPanel() {
		return formPanel;
	}




	public void setFormPanel(FormPanel formPanel) {
		this.formPanel = formPanel;
	}




	public FormBinding getFormBindings() {
		return formBindings;
	}




	public void setFormBindings(FormBinding formBindings) {
		this.formBindings = formBindings;
	}




	public HtmlEditor getContent() {
		return content;
	}




	public void setContent(HtmlEditor content) {
		this.content = content;
	}




	
	
}
